package org.oos.model;

import java.io.InputStream;

import org.oos.OOS4Java;
import org.oos.atom.elements.Link;
import org.oos.atom.entries.MultimediaEntry;
import org.oos.dao.MultimediaDao;
import org.oos.dao.exceptions.DeleteException;
import org.oos.dao.exceptions.ReadException;
import org.oos.dao.exceptions.UpdateException;

public class Multimedia extends AbstractEntityImpl {
	private static final long serialVersionUID = -6257634945141350779L;

	private MultimediaEntry entry;
	private Review review;
	
	public Multimedia(OOS4Java oos4Java, MultimediaEntry entry) {
		super(oos4Java);
		this.entry = entry;
	}

	public MultimediaEntry getEntry() {
		return entry;
	}

	public void setEntry(MultimediaEntry entry) {
		this.entry = entry;
	}

	public Review getReview() {
		return review;
	}

	public void setReview(Review review) {
		this.review = review;
	}

	public String getDescription() {
		return entry.getDescription();
	}

	public void setDescription(String description) {
		entry.setDescription(description);
	}

	public String getFnv() {
		return entry.getFnv();
	}

	public boolean isVideo() {
		return entry.getContent().getType().equals("application/x-flash-video");
	}

	public String getContentUrl() {
		return entry.getContent().getSrc();
	}

	public String getSplashScreen() {
		if (isVideo()) {
			for (Link link : entry.getLinks()) {
				if (link.getRel().equals("splash")) {
					return link.getHref();
				}
			}
			return null;
		} else {
			return null;
		}
	}

	public void update() throws UpdateException {
		MultimediaDao dao = oos4Java.getMultimediaDao();
		dao.setUserSlug(review.getAuthor().getSlug());
		dao.setSiteSlug(review.getSite().getSlug());
		dao.update(entry);
	}

	public void delete() throws DeleteException {
		MultimediaDao dao = oos4Java.getMultimediaDao();
		dao.setUserSlug(review.getAuthor().getSlug());
		dao.setSiteSlug(review.getSite().getSlug());
		dao.deleteMedia(entry);
	}

	public InputStream getInputStream() throws ReadException {
		MultimediaDao dao = oos4Java.getMultimediaDao();
		dao.setUserSlug(review.getAuthor().getSlug());
		dao.setSiteSlug(review.getSite().getSlug());
		return dao.readMedia(getFnv());
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Multimedia) {
			Multimedia other = (Multimedia) obj;
			return other.getFnv().equals(getFnv());
		}
		return false;
	}
}
